<?php

namespace pam\Controller;

/**
 * Description of IndicatorController
 *
 * @author Robert Bwana <robert.bwana@ssra.go.tz>
 */
use Zend\Mvc\Controller\AbstractActionController as AAC;
use Zend\View\Model\ViewModel;
use Zend\Form\FormInterface;
use pam\Form\IndicatorForm;
use pam\Model\Indicator;
use pam\Service\PolicyServiceInterface as PSI;
use pam\Service\IndicatorsServiceInterface as ISI;

class IndicatorController extends AAC {

    protected $indicatorService;
    protected $form;
    protected $policyService;

    public function __construct(ISI $indicatorService,PSI $policyService, FormInterface $form) {
        $this->indicatorService = $indicatorService;
        $this->policyService = $policyService;
        $this->form = $form;
    }

    public function viewAction() {
        $policyId = $this->params('id');
        
        return new ViewModel(array(
            'policy' => $this->policyService->findPolicy($policyId),
            'indicators' => $this->indicatorService->findAllInPolicy($policyId)
        ));
    }

}

/*
class SocialPolicyController extends AAC {


    public function viewAction() {
        return new ViewModel(array(
            'socialpolicies' => $this->policyService->findAll()
        ));
    }

    public function addAction() {
        $request = $this->getRequest();

        if ($request->isPost()) {
            $this->socialForm->setData($request->getPost());

            if ($this->socialForm->isValid()) {
                try {

                    $this->policyService->savePolicy($this->socialForm->getData());

                    return $this->redirect()->toRoute('social');
                } catch (\Exception $e) {
                    die($e->getMessage());
                }
            }
        }

        return new ViewModel(array(
            'form' => $this->socialForm
        ));
    }
    
    public function editAction() {
         $request = $this->getRequest();
         try{
         $policy = $this->policyService->findPolicy($this->params('id'));
         }  catch (\InvalidArgumentException $e){
             return $this->redirect()->toRoute('social');
         }
         $this->socialForm->bind($policy);

         if ($request->isPost()) {
             $this->socialForm->setData($request->getPost());
             
             if ($this->socialForm->isValid()) {
                 try {
                     $this->policyService->savePolicy($policy);

                     return $this->redirect()->toRoute('social');
                 } catch (\Exception $e) {
                     die($e->getMessage());
                     // Some DB Error happened, log it and let the user know
                 }
             }
         }

         return new ViewModel(array(
             'form' => $this->socialForm
         ));
        
    }
    
    public function deleteAction() {
        try {
             $policy = $this->policyService->findPolicy($this->params('id'));
         } catch (\InvalidArgumentException $e) {
             return $this->redirect()->toRoute('social');
         }

         $request = $this->getRequest();

         if ($request->isPost()) {
             $del = $request->getPost('delete_confirmation', 'No');

             if ($del === 'Yes') {
                 $this->policyService->deletePolicy($policy);
             }

             return $this->redirect()->toRoute('social');
         }

         return new ViewModel(array(
             'policy' => $policy
         ));
        
    }

}
